//app.js
App({
  /*
    ### 公共缓存记录：
      wx.getStorageSync('change')  用于相册修改后的标识 ，以便其它相册浏览页面的数据及时更新,true标识为被修改，要及时更新
  
  */



  /*
    ### 后台服务器请求的身份验证
      参一：成功的回调
      说明：
        如果返回值为 ‘vc_err’ 说明验证失败
      示例：
        var t = this;
        this.Verify_Code(function(code){
          wx.request({
            url: t.globalData.urls,
            data:{
              vc:code
            },
            success(res){
              if(res != 'vc_err'){
                ......
              }
            }
          })
        });
  */
  VC(fun) {
    wx.request({
      url: this.globalData.urls,
      success(res, a, b) {
        // console.log(res);
        var date = new Date(res.header.Date).getTime() / 1000;
        var code = date + '_' + ((date + 3) * 2) + ((date - 1506) * 5);
        fun(code);
      }
    })
  },


  onLaunch: function () {
    var t = this;
    this.GetSystemInfo(function (res) { })


    //判断当前用户是否以保存信息记录，没有则保存用户信息
    this.getOpenid(function (openid) {
      if (openid) {
        t.getUserInfo(function (res) {
          t.VC(function(vc){
            wx.request({
              url: t.globalData.urls + '?GetTypes=Get_UserInfo',
              data: {
                openid: openid,
                vc:vc
              },
              success: function (res2) {
                if (res2.data.length < 1) {
                  t.VC(function(vc2){
                    wx.request({
                      url: t.globalData.urls + '?GetTypes=UserEdit',
                      data: {
                        openid: openid,
                        user_img: res.avatarUrl,
                        user_name: res.nickName,
                        vc:vc2
                      },
                      success(res3){
                        console.log(res3)
                      }
                    })
                  });
                }
              }
            })
          });
        });

      }
    });


  },






  /*
    ### 当相册封面失效时设置默认封面
      参一：图片加载时的e事件信息
      参二：t当前的对象
      需要： arr_left,arr_right 左右图片容器
  */
  SetDefaltCover(e, t) {
    var index = e.currentTarget.dataset.index;
    var side = e.currentTarget.dataset.side;
    var data;
    if (side == 'left') {
      data = t.data.arr_left;
      data[index].src = this.globalData.default_img;
      t.setData({
        arr_left: data
      });
    } else {
      data = t.data.arr_right;
      data[index].src = this.globalData.default_img;
      t.setData({
        arr_right: data
      });
    }
  },







  /*
    ### 获取公共相册分页数据
    ### 需要设置data数据
      pnow:1,     //当前页
      pcount:0,   //分页总数
      album_list:[],  //相册总容器
      arr_left:[],    //相册左右容器
      arr_right:[],

    ### 示例
      app.GetAllAlbum2(t, 'all',search,function(){})
      app.GetAllAlbum2(t, 'ok2370HB2eemmawMsiiAMIJrZFGg',search,function(){})

  */
  GetAllAlbum(t,album_type, search, fun) {
    var that = this
    if (t.data.pcount != 0 && t.data.pnow > t.data.pcount) {
      return;
    }
    that.VC(function(vc){
      if (album_type == 'all') {  //获取全部相册
        var data = {
          GetTypes: 'Get_All_Public_Album',
          pnow: t.data.pnow,
          pcount: t.data.pcount,
          type: 1,
          search: search,
          vc:vc
        }
      } else {    //获取指定用户的全部相册
        var data = {
          GetTypes: 'Get_User_Album',
          openid: album_type,
          pnow: t.data.pnow,
          vc: vc
        }
      }
      wx.request({
        url: that.globalData.urls,
        data: data,
        success(res) {
          var list = res.data.DataList
          var album_list = t.data.album_list;
          for (var i = 0; i < list.length; i++) {
            album_list.push(list[i]);
          }
          that.getOpenid(function (openid) {
            that.GetColletIds(openid, function (res2) {

              that.GetFlowerIds(openid, function (res3) {
                var left = [];
                var right = [];
                //获取用户点赞过的相册id集合，并判断每个相册是否被赞
                var flowers = res3.data
                for (var i = 0; i < album_list.length; i++) {
                  for (var c = 0; c < flowers.length; c++) {
                    if (album_list[i].Album_Id == flowers[c].Flower_Aid) {
                      album_list[i].isFlower = true;
                    }
                  }
                }
                //获取用户收藏的相册id集合，并判断每个相册是否被收藏
                var collects = res2.data;
                for (var i = 0; i < album_list.length; i++) {
                  for (var c = 0; c < collects.length; c++) {
                    if (album_list[i].Album_Id == collects[c].Collect_Aid) {
                      album_list[i].isCollect = true;
                    }
                  }
                  if (album_list[i].Album_Pic_Cover != '0') {
                    album_list[i].src = that.globalData.domain + album_list[i].Album_Pic_Cover;
                  }
                  //将所有数据分装到左右两个容器中
                  if (i % 2 == 0) {
                    left.push(album_list[i]);
                  } else {
                    right.push(album_list[i]);
                  }
                }
                t.setData({
                  album_list: album_list,
                  arr_left: left,
                  arr_right: right,
                  pnow: t.data.pnow + 1,
                  pcount: res.data.PageCount
                })
                fun();
              })
            })
          });
        }
      })

    });
    
  },












  /*
      ### 功能：获取图片信息并等比缩放后分装入两个容器中
      ### 需要 data 参数 
          pic_left: [],    //图片左右容器
          pic_right: [],

      ### 需要 image传入参数 
          pid 图片id
          src 图片全路径 
          psrc  图片原相对路

      ### 使用方法
        将指定图片集合使用image遍历出来，在image中添加事件触发本函数即可。

      ### 注意
        另忘了先使用 app.GetSystemInfo 获取屏幕宽度
  */
  Load_Info(e, t) {
    //获取图片信息并装载到临时对象
    var height = e.detail.height;  //图片原高
    var width = e.detail.width;    //图片原宽
    //等比缩放
    var scale = (t.data.screenWidth / 2) / width
    var sheight = scale * height
    var pid = e.target.dataset.pid;
    var src = e.target.dataset.src;
    var img = new Object;

    img.pid = pid;
    img.height = sheight;
    img.src = src;
    img.psrc = e.currentTarget.dataset.psrc
    var pre_img_list = t.data.pre_img_list;
    pre_img_list.push(src);
    //获取左右图片容器数据并计算高度，将图片对象存放到高度小的那个容器中
    var pic_left = t.data.pic_left
    var pic_right = t.data.pic_right
    var height_left = 0;
    var height_right = 0;
    for (var i = 0; i < pic_left.length; i++) {
      height_left += pic_left[i].height
    }

    for (var i = 0; i < pic_right.length; i++) {
      height_right += pic_right[i].height
    }

    if (height_right < height_left) {
//      pic_right.unshift(img)
      pic_right.push(img)
      t.setData({
        pic_right: pic_right,
        pre_img_list: pre_img_list
      })
    } else {
     // pic_left.unshift(img)
      pic_left.push(img)
      t.setData({
        pic_left: pic_left,
        pre_img_list: pre_img_list
      })
    }
  },








  /*
    ### 功能：获取相册图片的分页数据
      //需要data中设置的参数
      pcount:0,   //分页总数
      pnow:1,     //分页当前页
      album_id:?,
      pic_left: [],    //图片左右容器
      pic_right: [],
      pic_list:[],  //相片临时总容器
      pre_img_list:[],  //全屏预览图片容器
      结果：将获取到的分页数据赋值给 data.pic_list
  */
  GetPictureFpage(t){
    var that = this
    t.setData({
      pic_list: []
    })
    if (t.data.pcount != 0 && t.data.pnow > t.data.pcount) {
      return
    }
    var album_id = t.data.album_id;
    //var pre_img_list = t.data.pre_img_list;
    that.VC(function(vc){
      wx.request({
        url: that.globalData.urls,
        data: {
          GetTypes: 'Get_Pictures',
          album_id: album_id,
          pnow: t.data.pnow,
          vc:vc
        },
        success: function (res) {
          var list = res.data.DataList;
          var pic_list = t.data.pic_list;
          for (var i = 0; i < list.length; i++) {
            list[i].src = that.globalData.domain + list[i].Picture_Src  //拼接图片src全地址
            //pic_list.push(list[i]);
            pic_list.unshift(list[i]);
          }
          t.setData({
            pcount: res.data.PageCount,
            pnow: t.data.pnow + 1,
            pic_list: pic_list,
            //pre_img_list: pre_img_list
          });
        }
      })
    });
  },






  //对相册点击赞事件
  SetGood(openid,album_id,fun) {
    var t = this;
    t.VC(function(vc){
      wx.request({
        url: t.globalData.urls,
        data: {
          GetTypes: 'Album_Flower_Add',
          Openid: openid,
          Album_Id: album_id,
          vc:vc
        },
        success(res) {
          fun(res.data)
        }
      });
    });
  },








  //用户收藏或取消相册的收藏
  Album_Collet(album_id, openid,fun) {
    var t = this;
    t.VC(function(vc){
      wx.request({
        url: t.globalData.urls,
        data: {
          GetTypes: 'Collect_Add',
          Album_Id: album_id,
          Openid: openid,
          vc:vc
        },
        success(res) {
          fun(res)
        }
      })
    });
  },





  //获取指定用户的收藏相册id集合
  GetColletIds(openid,fun){
    var t = this
    t.VC(function(vc){
      wx.request({
        url: t.globalData.urls,
        data: {
          GetTypes: 'Get_Collect_Albums2',
          openid: openid,
          vc:vc
        },
        success(res) {
          fun(res)
        }
      })
    });
  },




  //获取指定用户以赞相册id集合
  GetFlowerIds(openid, fun) {
    var t = this
    t.VC(function(vc){
      wx.request({
        url: t.globalData.urls,
        data: {
          GetTypes: 'Get_Flower_Albums',
          openid: openid,
          vc:vc
        },
        success(res) {
          fun(res)
        }
      })
    });
  },



  //获取相册作者信息
  GetAuthor(t, album_openid,fun) {
    var t = this
    t.VC(function(vc){
      wx.request({
        url: t.globalData.urls,
        data: {
          GetTypes: 'Get_UserInfo',
          openid: album_openid,
          vc:vc
        },
        success: function (res) {
          fun(res);
        }
      })
    });
  },

  //判断相册是否被用户赞(res返回大于0,代表被赞)
  CheckAlbumIsFlower(album_id, openid,fun){
    var t = this
    t.VC(function(vc){
      wx.request({
        url: t.globalData.urls,
        data: {
          GetTypes: 'CheckAlbumIsFlower',
          album_id: album_id,
          openid: openid,
          vc:vc
        },
        success(res) {
          fun(res.data)
        }
      })
    });
  },


  //判断相册是否被用户收藏(res返回大于0,代表被收藏)
  CheckAlbumIsCollect(album_id, openid, fun) {
    var t = this
    t.VC(function(vc){
      wx.request({
        url: t.globalData.urls,
        data: {
          GetTypes: 'Album_Is_Collect',
          album_id: album_id,
          openid: openid,
          vc:vc
        },
        success(res) {
          fun(res.data);
        }
      })
    });
  },



  //点击相册时跳转到相册详情页面
  Jump_Detail(album_id) {
    wx.navigateTo({
      url: '../detail/detail?album_id=' + album_id
    })
  },

 

  /*
    功能：获取当前手机系统信息
    示例：
      app.GetSystemInfo(function(res){
        console.log(res)
      })
  */
  GetSystemInfo(fun){
    var t = this
    if(t.globalData.systemInfo == ''){
      wx.getSystemInfo({
        success: function (res) {
          t.globalData.systemInfo = res;
          fun(t.globalData.systemInfo)
        }
      })
    }else{
      fun(t.globalData.systemInfo)
    }
  },





  /*
    ### 获取用户Openid，当获取到信息时会缓存到本地
    ### 示例：
      this.getOpenid(function(openid){
      app.getOpenid(function(openid){
        console.log(openid);
      });
   */
  getOpenid(cb) {
    var t = this
    if (this.globalData.openid) {
      cb(this.globalData.openid);
    }else{    
      wx.login({
        success: function (res) {
          var appid = 'wx53d35d56d39587c2'
          var secret = '2546b893b238ff0ee24e014039d1f58b'
          var ourl = t.globalData.urls
          if (res.code) {
            t.VC(function(vc){
              wx.request({
                url: ourl,
                data: {
                  GetTypes:'Get_Openid',
                  code:res.code,
                  vc:vc
                },
                success: function (res) {
                  t.globalData.openid = res.data.openid;
                  cb(t.globalData.openid);
                }
              })
            });
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        },
        fail:function(r){
          console.log(r)
        }
      })
    }
  },


  /*
    ###获取当前登陆用户信息
    ###调用示例：
      var t = this;
      this.getUserInfo(function(res){
      app.getUserInfo(function(res){
        t.setData({
          userInfo:res
        })
        console.log(t.data.userInfo)
      });
  */
  getUserInfo: function (cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            withCredentials: true,
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },



  globalData: {
    systemInfo:'',
    default_img: '../../img/a1.jpg',  //全局默认使用图片
    userInfo: null,
    album_left:[], //遍历相册的左右容器
    album_right:[],
    openid:null,
    //url: 'http://photo.tobeyoung.net/Album.aspx',
    //urls: 'http://photo.tobeyoung.net/Album.aspx',   //安全https域名
    //domain: 'http://photo.tobeyoung.net',
    //System_Img_Url: 'https://photo.tobeyoung.net/Sites/Site34/Album/system/',

    //url: 'http://album.bmxx8.top/Album.aspx',   
    //urls: 'http://album.bmxx8.top/Album.aspx', 
    //domain: 'http://album.bmxx8.top',
    //System_Img_Url: 'https://album.bmxx8.top/Sites/Site34/Album/system/'   

    url: 'http://www.es.net/Album.aspx',
    urls: 'http://www.es.net/Album.aspx',   //安全https域名
    domain: 'http://www.es.net',
    //System_Img_Url: 'http://www.es.net:81/Sites/Site34/Album/system/',
 
  },




})